<?php

namespace app\common\model;
use think\Db;
class Report extends \think\Model{
    protected $table='up_report';

    /**
     * @param $pageSize
     * @param $search
     * @return \think\Paginator
     * 列表数据
     */
    public function relData($pageSize,$search){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['a.report_aid|a.report_id|uuu.user_username|uuu.user_rel_name|uuu1.user_rel_name']=['like',"%$search[0]%"];
        }
        if($search[1] != '' && $search[2] !=''){
            $where['a.report_time'] = array('between',array($search[1],$search[2]));
        }else if($search[1] != ''){
            $where['a.report_time'] = array('EGT',$search[1]);
        }else if($search[2]!=''){
            $where['a.report_time'] = array('ELT',$search[2]);
        }else{
            $where['a.report_time'] = array('EGT',0);
        }
        $where['uar.a_del']=1;
        $list = $model->alias('a')
            ->join('up_u_user uuu','a.report_user_id = uuu.user_id','left')//举报人
            ->join('up_u_user uuu1','a.report_cover_user_id = uuu1.user_id','left')//被举报人
            ->join('up_admin ua','a.report_handle = ua.id','left')
            ->join('up_report_name urn', 'a.report_type = urn.report_type_id', 'left')
            ->join('up_article uar','uar.a_id = a.report_aid','left')
            ->where($where)
            ->field('a.*,uuu.user_phone,uuu.user_username,uuu1.user_phone as phone1,uuu1.user_username as user_name1,urn.report_name')
            ->order('a.report_time desc')
            ->paginate($pageSize);
        return $list;
    }

    /***
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * 查询详情
     */
    public function findOne($id){
        $model = self::instance();
        $where['a.report_id'] = $id;
        $list = $model->alias('a')
                        ->join('up_u_user uuu','a.report_user_id = uuu.user_id','left')//举报人
                        ->join('up_u_user uuu1','a.report_cover_user_id = uuu1.user_id','left')//被举报人
                        ->join('up_admin ua','a.report_handle = ua.id','left')
                        ->join('up_report_name urn', 'a.report_type = urn.report_type_id', 'left')
                        ->join('up_article uar','uar.a_id =a.report_aid','left')
                        ->join('up_area uaa','uaa.district_id = uar.a_shi','left')
                        ->where($where)

            ->field(
                    'uaa.district,
                    uar.a_num,uar.a_createtime,uar.a_auth_type,uar.a_title,uar.a_content,uar.a_auth_city,
                    a.*,
                    uuu.user_phone,uuu.user_username,
                    uuu1.user_phone as phone1,uuu1.user_username as user_name1,
                    urn.report_name')
                    ->find();
                if($list['a_auth_type']==1){
                    $list['auth_type'] = '普通';
                }elseif ($list['a_auth_type']==2){
                    $list['auth_type'] = '铜牌';
                }elseif ($list['a_auth_type']==3){
                    $list['auth_type'] = '银牌';
                }elseif ($list['a_auth_type']==4){
                    $list['auth_type'] = '金牌';
                }else{
                    $list['auth_type'] = '';
                }
                if($list['a_auth_city']!=0){
                    $district = Db::table('up_area')->where('district_id',$list['a_auth_city'])->value('district');
                    $list['fb_district'] = $district;
                }else{
                    $list['fb_district'] = '全国';
                }
        return $list;
    }
    private static $instance;

    public static function instance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}